package com.itheima.youdianda.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.common.ResultData;
import com.itheima.youdianda.entity.SysMenu;
import com.itheima.youdianda.exception.BusinessException;
import com.itheima.youdianda.service.SysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * lxl菜单管理
 */
@RestController
@Slf4j
@RequestMapping("/sys_menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    /***
     * 菜单所有分级查询
     * @return
     */
    @GetMapping("/allData")
    public ResultData allDate() {
        //按排序查询
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(SysMenu::getSort);
        List<SysMenu> sysMenuList = sysMenuService.list(queryWrapper);
        //1.查询根节点数据
        List<SysMenu> rootList = sysMenuList.stream().filter(sysMenu ->
                sysMenu.getPid().equals(0)
        ).collect(Collectors.toList());

        List<SysMenu> sysMenus = rootList.stream().map(root -> {
            root.setChildren(getChildren(root, sysMenuList));
            return root;
        }).collect(Collectors.toList());
        return ResultData.ok("", sysMenus);
    }

    private List<SysMenu> getChildren(SysMenu root, List<SysMenu> sysMenuList) {
        List<SysMenu> childrens = new ArrayList<>();// 子菜单
        for (SysMenu sysMenu : sysMenuList) {
            if (sysMenu.getPid().equals(root.getId())) {
                childrens.add(sysMenu);
            }
        }
        if (CollectionUtils.isNotEmpty(childrens)) {
            for (SysMenu sysMenu : childrens) {
                List<SysMenu> children_2 = getChildren(sysMenu, sysMenuList);
                sysMenu.setChildren(children_2);
            }
            return childrens;
        }
        return Collections.emptyList();
    }


    /****
     * 修改菜单回显数据
     * @param sysMenu
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(SysMenu sysMenu){
        log.info("传来的id是:{}" , sysMenu.getId());
        SysMenu sysm = sysMenuService.getById(sysMenu.getId());
        return ResultData.ok("",sysm);
    }

    /*****
     * 修改信息
     * @param sysMenu
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody SysMenu sysMenu){
        sysMenuService.updateById(sysMenu);
        return R.success("修改成功","");
    }

    /****
     * 添加菜单
     * @param sysMenu
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody SysMenu sysMenu){
          sysMenuService.save(sysMenu);
          return R.success("添加成功","");
    }

    @GetMapping("/del")
    public ResultData delete(SysMenu sysMenu){
        //判断删除的数据是否是默认数据
        SysMenu menu1 = sysMenuService.getById(sysMenu.getId());
        if ("1".equals(menu1.getDefaultData())){
            throw new BusinessException("系统默认数据暂时无法删除");
        }

        //查询所有菜单信息
        List<SysMenu> sysMenuList = sysMenuService.list();
        //1.判断删除的是菜单还是按钮,按钮直接删除
        List<SysMenu> sysMenus = sysMenuList.stream().filter(menu -> menu.getId().equals(sysMenu.getId())).collect(Collectors.toList());
        if(sysMenus.get(0).getType() == 2){
            sysMenuService.removeById(sysMenu.getId());
            return ResultData.ok("删除按钮成功","");
        }
        //根据传来的id查询是否有子菜单,  如果有不能删除
        List<SysMenu> children = getChildren(sysMenu, sysMenuList);
        if(CollectionUtils.isNotEmpty(children)){
            // 判断 子菜单的类型
            for (SysMenu child : children) {
                if(child.getType() == 1){
                    return ResultData.error("该菜单下有子菜单,暂无法删除");
                }
            }
           //子菜单都是按钮,连同按钮一起删除
            List<Integer> ids = children.stream().map(child -> {
                return child.getId();
            }).collect(Collectors.toList());
            ids.add(sysMenu.getId());
            sysMenuService.removeByIds(ids);
            return ResultData.ok("删除菜单和按钮成功","");
        }
        sysMenuService.removeById(sysMenu.getId());
        return ResultData.ok("删除菜单成功","");
    }
}
